package com.gitee.lougnib.simplekits.quick_generator.service;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.gitee.lougnib.simplekits.common_impls.jackson.JacksonKit;
import com.gitee.lougnib.simplekits.common_impls.velocity2.Velocity2Kit;
import com.gitee.lougnib.simplekits.common_impls.velocity2.Velocity2TemplateService;
import com.gitee.lougnib.simplekits.quick_generator.Args;
import com.google.common.io.Files;

public class SingleTemplateQuickGenerator {

	private Velocity2TemplateService tmpService = new Velocity2TemplateService();

	public void run(Args args) throws IOException {

		// 输出目录
		File outputDirectory = new File("output");
		if (StringUtils.isNotBlank(args.getDirectory())) {
			outputDirectory = new File(args.getDirectory());
		}

		// 输入数据
		String json = getJson(args);
		Map<String, Object> context = JacksonKit.toObject(json, new TypeReference<Map<String, Object>>(){});

		// 输入模板
		String result = "";
		File templateFile = new File(args.getTemplate());
		if (templateFile.exists()) {
			if (templateFile.isFile()) {
				mergeFile(templateFile, context, outputDirectory, args.getFile());
			} else if (templateFile.isDirectory()) {
				File[] tmpFiles = templateFile.listFiles(new FilenameFilter() {

					@Override
					public boolean accept(File dir, String name) {
						return name.toLowerCase().endsWith("json");
					}

				});

				if (tmpFiles != null) {
					for (File tmpFile : tmpFiles) {
						mergeFile(tmpFile, context, outputDirectory, args.getFile());
					}
				}
			}
		} else {
			result = Velocity2Kit.mergeStringTemplate(args.getTemplate(), context);
			String file = Velocity2Kit.mergeStringTemplate(args.getFile(), context);
			File outputFile = new File(outputDirectory, file);
			if (!outputFile.getParentFile().exists()) {
				outputFile.getParentFile().mkdirs();
			}
			Files.asByteSink(outputFile).write(result.getBytes("UTF-8"));
		}
	}

	private void mergeFile(File templateFile, Map<String, Object> context, File outputDirectory, String fileName) {
		String file = Velocity2Kit.mergeStringTemplate(fileName, context);
		File outputFile = new File(outputDirectory, file);
		if (!outputFile.getParentFile().exists()) {
			outputFile.getParentFile().mkdirs();
		}

		tmpService.setVelocityEngine(Velocity2Kit.getFileVelocityEngine(templateFile.getParentFile().getAbsolutePath()));
		tmpService.merge(templateFile.getName(), context, outputFile);
	}

	private String getJson(Args args) throws IOException {
		String json = "{}";
		if (args.getJson().trim().startsWith("{")) {
			json = args.getJson();
		} else {
			File jsonFile = new File(args.getJson());
			if (jsonFile.exists()) {
				json = Files.asCharSource(jsonFile, Charset.forName("UTF-8")).read();
			} else {
				throw new RuntimeException("文件不存在：" + args.getJson());
			}
		}

		return json;
	}
}
